package com.zhiche.lisa.bms.controller.fee;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.fee.FeeReceivable;
import com.zhiche.lisa.bms.pojo.dto.util.LspBasicDTO;
import com.zhiche.lisa.bms.service.fee.FeeReceivableService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 应收账款 前端控制器
 * </p>
 *
 * @author qichao
 * @since 2018-09-03
 */
@RestController
@RequestMapping(value = "/feeReceivable")
@Api(value = "/feeReceivable", description = "应收账款列表接口", tags = {"应收账款列表接口"})
public class FeeReceivableController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FeeReceivableController.class);
    @Autowired
    private FeeReceivableService feeReceivableService;

    /**
     * 获取应收账款列表
     *
     * @param page
     * @return
     */
    @PostMapping(value = "/lisaFeeReceivableLine")
    @ApiOperation(value = "获取应收账款列表", notes = "获取应收账款列", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Page<FeeReceivable>> lisaFeeReceivableLine(@RequestBody Page<FeeReceivable> page) {
        LOGGER.info("FeeReceivableController.lisaFeeReceivableLine param: {}", page);
        RestfulResponse<Page<FeeReceivable>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            Page<FeeReceivable> pageVo = feeReceivableService.lisaFeeReceivableLinePage(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("FeeReceivableController.lisaFeeReceivableLine error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeReceivableController.lisaFeeReceivableLine error:{}", e.getMessage());
            result.setMessage("获取应收账款异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 获取合同名称
     *
     * @return
     */
    @GetMapping(value = "/selectContractCode")
    @ApiOperation(value = "获取合同编码", notes = "获取合同编码", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<FeeReceivable>> selectContractCode() {
        RestfulResponse<List<FeeReceivable>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            List<FeeReceivable> lisaFeeReceivables = feeReceivableService.selectContractCode();
            result.setData(lisaFeeReceivables);
        } catch (BaseException e) {
            LOGGER.error("FeeReceivableController.selectContractName error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeReceivableController.selectContractName error:{}", e.getMessage());
            result.setMessage("获取合同编码失败");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 获取承运商名称
     */
    @GetMapping(value = "/queeryLspNameByLspId")
    @ApiOperation(value = "获取承运商名称", notes = "获取承运商名称", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<LspBasicDTO>> queeryLspNameByLspId(@RequestHeader("Authorization") String authorization) {
        RestfulResponse<List<LspBasicDTO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            List<LspBasicDTO> listString = feeReceivableService.queeryLspNameByLspId(authorization);
            result.setData(listString);
        } catch (BaseException e) {
            LOGGER.error("FeeReceivableController.queeryLspNameByLspId error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeReceivableController.queeryLspNameByLspId error:{}", e.getMessage());
            result.setMessage("获取承运商名称失败");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 新增应收账款
     */
    @PostMapping(value = "/insertFeeReceivablebms")
    @ApiOperation(value = "新增应收账款", notes = "新增应收账款", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<String> insertFeeReceivablebms(@RequestBody List<FeeReceivable> lisaFeeReceivable, @RequestHeader("Authorization") String token) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            feeReceivableService.insertFeeReceivablebms(lisaFeeReceivable, token);
        } catch (BaseException e) {
            LOGGER.error("FeeReceivableController.insertFeeReceivablebms error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeReceivableController.insertFeeReceivablebms error:{}", e.getMessage());
            result.setMessage("新增应收账款");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }
}

